﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WSS.hameduconn.Business.Dto;

namespace WSS.hameduconn.Business.Api
{
    public class CheckedLoginApi
    {
        private static WSShameduconnDBEntities contextDB = new WSShameduconnDBEntities();
        public static CheckedLoginDto CheckedLogin(string username, string password) 
        {
            CheckedLoginDto checkedLogin = new CheckedLoginDto();
            try {
                Registration objRegistration = contextDB.Registrations.First(attr => attr.Username == username && attr.Password == password);
                checkedLogin.IsSuccess = true;
                checkedLogin.Permission = 0;
                checkedLogin.UserId = objRegistration.Id;
                checkedLogin.UserName = objRegistration.Username;
                checkedLogin.Message = "Login is successful";
                return checkedLogin;
            }
            catch (Exception ex) {
                try {
                    Login objLogin = contextDB.Logins.First(attr => attr.Username == username && attr.Password == password);
                    checkedLogin.IsSuccess = true;
                    checkedLogin.Permission = 1;
                    checkedLogin.UserId = objLogin.Id;
                    checkedLogin.UserName = objLogin.Username;
                    checkedLogin.Message = "Login is successful";
                    return checkedLogin;
                }
                catch (Exception exx) {
                    checkedLogin.IsSuccess = false;
                    checkedLogin.Message = "Username or Password not correct";
                    return checkedLogin;
                }                
            }
        }

        public static CheckedLoginDto CheckedUsername(string username)
        {
            CheckedLoginDto checkedLogin = new CheckedLoginDto();
            try
            {
                Registration objRegistration = contextDB.Registrations.First(attr => attr.Username == username);
                checkedLogin.IsSuccess = false;
                checkedLogin.Message = "Account is ready";
                return checkedLogin;
            }
            catch (Exception ex)
            {
                try
                {
                    Login objLogin = contextDB.Logins.First(attr => attr.Username == username);
                    checkedLogin.IsSuccess = false;
                    checkedLogin.Message = "Account is ready";
                    return checkedLogin;
                }
                catch (Exception exx)
                {
                    checkedLogin.IsSuccess = true;                    
                    return checkedLogin;
                }
            }
        }
    }
}
